package cn.iocoder.yudao.module.famous.doctor.controller.admin.certificate;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.certificate.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.certificate.FamCertificateDO;
import cn.iocoder.yudao.module.famous.doctor.convert.certificate.FamCertificateConvert;
import cn.iocoder.yudao.module.famous.doctor.service.certificate.FamCertificateService;

@Tag(name = "管理后台 - 互联网备案")
@RestController
@RequestMapping("/famous.doctor/fam-certificate")
@Validated
public class FamCertificateController {

    @Resource
    private FamCertificateService famCertificateService;

    @PostMapping("/create")
    @Operation(summary = "创建互联网备案")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-certificate:create')")
    public CommonResult<Long> createFamCertificate(@Valid @RequestBody FamCertificateCreateReqVO createReqVO) {
        return success(famCertificateService.createFamCertificate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新互联网备案")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-certificate:update')")
    public CommonResult<Boolean> updateFamCertificate(@Valid @RequestBody FamCertificateUpdateReqVO updateReqVO) {
        famCertificateService.updateFamCertificate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除互联网备案")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-certificate:delete')")
    public CommonResult<Boolean> deleteFamCertificate(@RequestParam("id") Long id) {
        famCertificateService.deleteFamCertificate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得互联网备案")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-certificate:query')")
    public CommonResult<FamCertificateRespVO> getFamCertificate(@RequestParam("id") Long id) {
        FamCertificateDO famCertificate = famCertificateService.getFamCertificate(id);
        return success(FamCertificateConvert.INSTANCE.convert(famCertificate));
    }

    @GetMapping("/list")
    @Operation(summary = "获得互联网备案列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-certificate:query')")
    public CommonResult<List<FamCertificateRespVO>> getFamCertificateList(@RequestParam("ids") Collection<Long> ids) {
        List<FamCertificateDO> list = famCertificateService.getFamCertificateList(ids);
        return success(FamCertificateConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得互联网备案分页")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-certificate:query')")
    public CommonResult<PageResult<FamCertificateRespVO>> getFamCertificatePage(@Valid FamCertificatePageReqVO pageVO) {
        PageResult<FamCertificateDO> pageResult = famCertificateService.getFamCertificatePage(pageVO);
        return success(FamCertificateConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出互联网备案 Excel")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-certificate:export')")
    @OperateLog(type = EXPORT)
    public void exportFamCertificateExcel(@Valid FamCertificateExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FamCertificateDO> list = famCertificateService.getFamCertificateList(exportReqVO);
        // 导出 Excel
        List<FamCertificateExcelVO> datas = FamCertificateConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "互联网备案.xls", "数据", FamCertificateExcelVO.class, datas);
    }

}
